package com.atguigu.chapter07.d_state;

import com.atguigu.chapter5.source.WaterSensor;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

/**
 * @ClassName: Flink01_State_Operator_List
 * @Description:
 * @Author: kele
 * @Date: 2021/4/7 19:38
 *
 *
 * 键控状态之 ListState
 *
 * 通过runtime获取
 *
 * 案例：针对每个传感器输出最高的3个水位值
 * 思路：使用list
 *
 * 1、先重写open方法，获取状态
 *       获取状态：liststate = getRuntimeContext()
 *                                 .getListState(new ListStateDescriptor< >( ));
 *
 * 2、数据处理更新状态
 *      liststate.add()       向liststate中添加值
 *      liststate.update()    更新list中的值
 *
 **/
public class Flink04_Key_State_List {

    public static void main(String[] args) {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port",20000);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        env.socketTextStream("hadoop162",8888)
                .map(datas->{

                    String[] data = datas.split(",");
                    return new WaterSensor(data[0],
                            Long.parseLong(data[1]) * 1000,
                            Integer.valueOf(data[2]));
                })
                .keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {

                    private ListState<Integer> liststate;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        liststate = getRuntimeContext()
                                .getListState(new ListStateDescriptor<Integer>("liststate", Integer.class));
                    }

                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {

                        //1、现将数据添加到list集合中
                        liststate.add(value.getVc());

                        ArrayList<Integer> list = new ArrayList<>();

                        /*if (liststate.get().iterator().hasNext()) {

                            list.add(liststate.get().iterator().next());
                        }*/

                        for (Integer integer : liststate.get()) {
                            list.add(integer);
                        }

                        //对集合的数据进行排序
                        list.sort(Comparator.reverseOrder());

                        if(list.size() > 3){
                            //如果集合中的数据超过3，则将第四个数据删除
                            list.remove(3);
                        }

                        liststate.update(list);

                        //可以将数据打印到控制台显示
                        out.collect("结果为：" + list.toString());
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
